﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    private oAuthOrkut _oauth = new oAuthOrkut();

    /// <summary>
    /// Page OnLoad Request
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        string oauth_token = Request.QueryString["oauth_token"];
        string oauth_verifier = Request.QueryString["oauth_verifier"];
        if (oauth_token != null && oauth_verifier != null)
        {
            txtRequestToken.Text = Application["reuqestToken"].ToString();
            txtTokenSecret.Text = Application["reuqestTokenSecret"].ToString();
            hypAuthToken.NavigateUrl = Application["oauthLink"].ToString();
            hypAuthToken.Text = Application["oauthLink"].ToString();

            Application["oauth_token"] = oauth_token;
            Application["oauth_verifier"] = oauth_verifier;

            txtoAuth_token.Text = oauth_token;
            txtoAuth_verifier.Text = oauth_verifier;

        }
    }

    /// <summary>
    /// On Click Event for Getting Authorization
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCreateAuthorization_Click(object sender, EventArgs e)
    {
        string authLink = _oauth.AuthorizationLinkGet();
        Application["reuqestToken"] = _oauth.Token;
        Application["reuqestTokenSecret"] = _oauth.TokenSecret;
        Application["oauthLink"] = authLink;

        txtRequestToken.Text = _oauth.Token;
        txtTokenSecret.Text = _oauth.TokenSecret;
        hypAuthToken.NavigateUrl = authLink;
        hypAuthToken.Text = authLink;
    }

    /// <summary>
    /// Get Access Token for the user
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnGetAccessToken_Click(object sender, EventArgs e)
    {
        _oauth.Token = txtoAuth_token.Text;
        _oauth.TokenSecret = txtTokenSecret.Text;
        _oauth.Verifier = txtoAuth_verifier.Text;

        _oauth.AccessTokenGet(txtoAuth_token.Text);
        txtAccessToken.Text = _oauth.Token;
        txtAccessTokenSecret.Text = _oauth.TokenSecret;


    }

    /// <summary>
    /// To retrieve Profile of the User
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void getProfile_Click(object sender, EventArgs e)
    {
        string data = "[{\"params\":{\"groupId\":\"@self\",\"userId\":\"@me\"},\"id\":\"0-p.g\",\"method\":\"people.get\"}]";
        string response = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
        if (response.Length > 0)
        {
            AboutUser user = new AboutUser();
            Person person = user.getProfile(response);
            pic.ImageUrl = person.ThumbnailUrl;
            name.Text = "Hi ," + person.Name;
        }
        else
        {
            name.Text = "Error Occured";
        }

    }

    /// <summary>
    /// Update the Status of the User
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void updateStatus_Click(object sender, EventArgs e)
    {
        update.Text = "Request Made";
        string data = "[{\"params\":{\"person\":{\"status\":\"" + status.Text + "\"},\"groupId\":\"@self\",\"userId\":\"@me\"},\"id\":\"1-p.u\",\"method\":\"people.update\"}]";
        string response = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
        if (response.Length > 0)
        {
            update.Text = "status updated";
        }
    }

    /// <summary>
    /// Smiley Insertion in Status of the User
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void smile_Click(object sender, CommandEventArgs e)
    {
        string caller = e.CommandArgument.ToString();
        if (caller.Equals("bigSmile"))
            status.Text += "[:D] ";
        else if (caller.Equals("smile"))
            status.Text += "[:)] ";
        else if (caller.Equals("angry"))
            status.Text += "[:x] ";
        else if (caller.Equals("wink"))
            status.Text += "[;)] ";
        else if (caller.Equals("surprise"))
            status.Text += "[:o] ";
        else if (caller.Equals("confuse"))
            status.Text += "[/)] ";
        else if (caller.Equals("funny"))
            status.Text += "[:P] ";
        else if (caller.Equals("sad"))
            status.Text += "[:(] ";
        else if (caller.Equals("cool"))
            status.Text += "[8)] ";
    }

    /// <summary>
    /// Fetch the Scraps for the User
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnFetchScraps_Click(object sender, EventArgs e)
    {
        string data = "[{\"params\":{\"pageType\":\"first\",\"messageType\":\"public_message\",\"groupId\":\"@friends\",\"userId\":\"@me\",\"count\":" + noOfScraps.Text + "},\"id\":\"1-scrp.g\",\"method\":\"messages.get\"}]";
        string response = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
        if (response.Length > 0)
        {
            Scrap scrap = new Scrap();
            SocialMessage[] socialMessage = scrap.fetch(response, int.Parse(noOfScraps.Text));

            /////////////////////////////////////////////////////////////////
            //////////////////////Define Click Events////////////////////////
            /////////////////////////////////////////////////////////////////

            int no_of_messages = socialMessage.Length;

            Image[] image = new Image[no_of_messages];
            Label[] Name = new Label[no_of_messages];
            Label[] body = new Label[no_of_messages];
            Button[] deleteScrap = new Button[no_of_messages];
            Button[] replyScrap = new Button[no_of_messages];

            Label[] br1 = new Label[no_of_messages];
            Label[] br2 = new Label[no_of_messages];
            Label[] br3 = new Label[no_of_messages];
            Label[] br4 = new Label[no_of_messages];
            Label[] br5 = new Label[no_of_messages];

            for (int i = 0; i < no_of_messages; i++)
            {
                if (socialMessage[i] == null)
                    break;
                Name[i] = new Label();
                Name[i].ID = "name" + i;
                Name[i].Text = socialMessage[i].MPerson.Name;

                image[i] = new Image();
                image[i].ID = "image" + i;
                image[i].ImageUrl = socialMessage[i].MPerson.ThumbnailUrl;

                body[i] = new Label();
                body[i].ID = "body" + i;
                body[i].Text = socialMessage[i].Message;

                deleteScrap[i] = new Button();
                deleteScrap[i].ID = "delete" + i;
                deleteScrap[i].Text = "Delete Scrap";


                replyScrap[i] = new Button();
                replyScrap[i].ID = "reply" + i;
                replyScrap[i].Text = "Reply";


                br1[i] = new Label(); br1[i].Text = "<br />";
                br2[i] = new Label(); br2[i].Text = "<br />";
                br3[i] = new Label(); br3[i].Text = "<br />";
                br4[i] = new Label(); br4[i].Text = "<br />";
                br5[i] = new Label(); br5[i].Text = "<br />";

                placeHolder.Controls.Add(Name[i]);
                placeHolder.Controls.Add(br1[i]);
                placeHolder.Controls.Add(image[i]);
                placeHolder.Controls.Add(br2[i]);
                placeHolder.Controls.Add(body[i]);
                placeHolder.Controls.Add(br3[i]);
                placeHolder.Controls.Add(deleteScrap[i]);
                placeHolder.Controls.Add(replyScrap[i]);
                placeHolder.Controls.Add(br4[i]);
                placeHolder.Controls.Add(br5[i]);
            }
        }
        else
        {
            res.Text = "Error Occured in Scrap Fetch";
        }

    }

    /// <summary>
    /// Delete the Scrap
    /// Required -> Message ID
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDeleteScrap_Click(object sender, EventArgs e)
    {
        string data = "";//"[{\"params\":{\"messageType\":\"public_message\",\"groupId\":\"@self\",\"userId\":\"@me\",\"msgId\":\"" + MessageID + "\"},\"id\":\"2-scrp.d\",\"method\":\"messages.delete\"}]";
        string response = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
        if (response.Length > 0)
        {
            res.Text = "Scrap Deleted";
        }
        else
        {
            res.Text = "Error Occured in Scrap Delete";
        }
    }

    /// <summary>
    /// Reply to the Scrap 
    /// Required -> Friend ID , Message ID, Reply Text
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReplyScrap_Click(object sender, EventArgs e)
    {
        string data = "";// "[{\"params\":{\"messageType\":\"public_message\",\"groupId\":\"@self\",\"userId\":\"" + FriendID + "\",\"message\":{\"body\":\"" + ReplyText + "\"},\"msgId\":\"" + MessageID + "\"},\"id\":\"2-scrp.c\",\"method\":\"messages.create\"}]";
        string response = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
        if (response.Length > 0)
        {
            res.Text = "Scrap Replied";
        }
        else
        {
            res.Text = "Error Occured in Scrap Reply";
        }

    }

    /// <summary>
    /// Response to any JSON Request
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnResponse_Click(object sender, EventArgs e)
    {
        string data = request.Text;
        res.Text = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
    }
    protected void btnFriends_Click(object sender, EventArgs e)
    {
        string data = "[{\"params\":{\"groupId\":\"@friends\",\"startIndex\":0,\"count\":" + noOfFriends.Text + ",\"userId\":\"@me\"},\"id\":\"1-frnds.g\",\"method\":\"people.get\"}]";
        string response = _oauth.postWebRequest(txtAccessToken.Text, txtAccessTokenSecret.Text, data);
        if (response.Length > 0)
        {
            AboutUser user = new AboutUser();
            Person[] person = user.getFriends(response, int.Parse(noOfFriends.Text));

            /////////////////////////////////////////////////////////////////
            /////////////////////////////Edit Here///////////////////////////
            /////////////////////////////////////////////////////////////////

            int count = person.Length;
            Image[] friendPic = new Image[count];
            Label[] friendName = new Label[count];
            Label[] br1 = new Label[count];
            Label[] br2 = new Label[count];
            for (int i = 0; i < count; i++)
            {
                if (person == null)
                    break;

                friendName[i] = new Label();
                friendName[i].ID = "friendName" + i;
                friendName[i].Text = person[i].Name;

                friendPic[i] = new Image();
                friendPic[i].ID = "friendImage" + i;
                friendPic[i].ImageUrl = person[i].ThumbnailUrl;

                br1[i] = new Label(); br1[i].Text = "<br />";
                br2[i] = new Label(); br2[i].Text = "<br /> <br />";

                friends.Controls.Add(friendName[i]);
                friends.Controls.Add(br1[i]);
                friends.Controls.Add(friendPic[i]);
                friends.Controls.Add(br2[i]);

            }
        }
        else
        {
            name.Text = "Error Occured";
        }
    }
}
